#include <memory.hpp>

Memory * Memory::_instance = 0;

Memory::Memory() : countersBlock()
#ifdef __DEBUG_MEMORY__
    , balance(0)
#endif
{

}

Memory::~Memory()
{
    #ifdef __DEBUG_MEMORY__
    if( balance != 0 )
        throw Exception("Fatal error : Memory leak!!!",EXCEPTION_INFOS);
    #endif
}

Memory * Memory::getInstance()
{
    if( _instance )
        return _instance;
    else
        return (_instance=new Memory());
}

void     Memory::killInstance()
{
    if( _instance )
    {
        delete _instance;
        _instance = 0;
    }
}


Counter * Memory::newCounter()
{
    #ifdef __DEBUG_MEMORY__
        balance++;
        cout<<"["<<balance<<"]"<<"Alloc Counter"<<endl;
    #endif
    return countersBlock.newCounter();
}

void      Memory::delCounter(Counter *counter)
{
    #ifdef __DEBUG_MEMORY__
        balance--;
        cout<<"["<<balance<<"]"<<"Delet Counter"<<endl;
    #endif
    countersBlock.delCounter(counter);
}

